<?php
/**
 * Created by PhpStorm.
 * User: JYuans
 * Date: 2019/12/3
 * Time: 15:07
 */
namespace Bstage\Controller;

use Common\Model\CommonModel;
class MidExamineDownRelationController extends BstageController {

    /**
     * 考核结果列表接口
     */
    public function examineResultList(){
        //必要参数
        $mid_ex_id = $_REQUEST['mid_ex_id']?$_REQUEST['mid_ex_id']:$this->errorReturn("缺少中层考核ID");
        $depart_id = $_REQUEST['depart_id']?$_REQUEST['depart_id']:$this->errorReturn("缺少部门ID");

        //查看考核是否结束
        //$down_relation_status = M("MidExamineDownRelation")


        //分页
        $now_page = $_REQUEST['now_page']?$_REQUEST['now_page']:1;//默认第一页
        $page_size = $_REQUEST['page_size']?$_REQUEST['page_size']:50;//默认每页50条

        //查询条件
        $where = array();
        $where['depart_id'] = $depart_id;
        //筛选级别
        if($_REQUEST["search_rank"] == 3 || $_REQUEST["search_rank"] == 4){
            $where['rank'] = $_REQUEST["search_rank"];
        }else{
            $where['rank'] = ['in','3,4'];
        }
        //筛选干部名称
        if($_REQUEST["search_name"]){
            $where['name'] = ['like','%'.$_REQUEST['search_name'].'%'];
        }
        //筛选职务
        if($_REQUEST["search_station"]){
            $where['station'] = ['like','%'.$_REQUEST['search_station'].'%'];
        }

        $field = "user_id,depart_id,name,rank,station";

        $list_data = M("User")
            ->where($where)
            ->page($now_page,$page_size)
            ->order("user_id")
            ->field($field)
            ->select();

        if(!$list_data){
            $this->returnJson(503,"无数据");
        }

        $depart_name = M("Department")
            ->where(['depart_id'=>$depart_id])
            ->getField("name");

        foreach($list_data as $key=>$value){
            $list_data[$key]['depart_name'] = $depart_name;
            $list_data[$key]['rank'] = CommonModel::rank_str_array[$value['rank']];

            $examine_result = M("MidExamineDownRelation")
                ->where(['mid_ex_id'=>$mid_ex_id,'be_examine_user_id'=>$value['user_id'],'status'=>1])
                ->field("examine_result,score")
                ->select();
            $item_score_list = array();
            $amount_score = 0;
            foreach($examine_result as $key2=>$value2){
                $examine_result_1 = json_decode($value2['examine_result']);
                if($item_score_list){
                    for($i=0;$i<count($examine_result_1);$i++){
                        $item_score_list[$i] = bcadd($item_score_list[$i],$examine_result_1[$i],4);
                    }
                }else{
                    $item_score_list = $examine_result_1;
                }
                $amount_score = bcadd($amount_score,$value2['score'],4);
            }

            $examine_count = count($examine_result);
            for($i=0;$i<count($item_score_list);$i++){
                $item_score_list[$i] = round(bcdiv($item_score_list[$i],$examine_count,4),2);
            }
            $list_data[$key]['item_score'] = $item_score_list;
            $list_data[$key]['score'] = round(bcdiv($amount_score,$examine_count,4),2);
        }

        $count = M("User")
            ->where($where)
            ->count();

        $examine_title_list = M("MidExamineItem")
            ->where(['mid_ex_id'=>$mid_ex_id])
            ->getField("title",true);

        $return_data = array();
        $return_data['list_data'] = $list_data;
        $return_data['examine_title_list'] = $examine_title_list;
        $return_data['count'] = $count;
        $return_data['now_page'] = $now_page;
        $return_data['page_size'] = $page_size;
        $return_data['max_page'] = ceil($count / $page_size);

        $this->successReturn("获取成功",$return_data);
    }

    /**
     * 获取得分汇总列表接口
     */
    public function scoreDetailsList(){
        //必要参数
        $mid_ex_id = $_REQUEST['mid_ex_id']?$_REQUEST['mid_ex_id']:$this->errorReturn("缺少中层考核ID");
        $depart_id = $_REQUEST['depart_id']?$_REQUEST['depart_id']:$this->errorReturn("缺少部门ID");
        //分页
        $now_page = $_REQUEST['now_page']?$_REQUEST['now_page']:1;//默认第一页
        $page_size = $_REQUEST['page_size']?$_REQUEST['page_size']:50;//默认每页50条
        //查询条件
        $where = array();
        $where['dr.mid_ex_id'] = $mid_ex_id;
        $where['dr.status'] = 1;
        $where['dr.depart_id'] = $depart_id;
        //筛选级别
        if($_REQUEST["search_rank"]){
            $where['u.rank'] = $_REQUEST["search_rank"];
        }
        //筛选干部名称
        if($_REQUEST["search_name"]){
            $where['u.name'] = ['like','%'.$_REQUEST['search_name'].'%'];
        }
        //筛选职务
        if($_REQUEST["search_station"]){
            $where['u.station'] = ['like','%'.$_REQUEST['search_station'].'%'];
        }

        $field = "u.user_id,u.depart_id,u.name,u.rank,u.station,dr.examine_time,dr.examine_result,dr.score";

        $depart_name = M("Department")
            ->where(['depart_id'=>$depart_id])
            ->getField("name");

        $list_data = M("MidExamineDownRelation")
            ->alias("dr")
            ->join("__USER__ u on dr.be_examine_user_id = u.user_id")
            ->where($where)
            ->field($field)
            ->page($now_page,$page_size)
            ->select();
        if(!$list_data){
            $this->returnJson(503,"无数据");
        }
        foreach($list_data as $key=>$value){
            $list_data[$key]['score'] = floatval($value['score']);

            $list_data[$key]['depart_name'] = $depart_name;

            $list_data[$key]['rank'] = CommonModel::rank_str_array[$value['rank']];

            $list_data[$key]['examine_time'] = changeDateTime($value['examine_time']);

            $list_data[$key]['examine_result'] = json_decode($value['examine_result'],true);
        }

        $count = M("MidExamineDownRelation")
            ->alias("dr")
            ->join("__USER__ u on dr.be_examine_user_id = u.user_id")
            ->where($where)
            ->count();

        $examine_title_list = M("MidExamineItem")
            ->where(['mid_ex_id'=>$mid_ex_id])
            ->getField("title",true);

        $return_data = array();
        $return_data['list_data'] = $list_data;
        $return_data['examine_title_list'] = $examine_title_list;
        $return_data['count'] = $count;
        $return_data['now_page'] = $now_page;
        $return_data['page_size'] = $page_size;
        $return_data['max_page'] = ceil($count / $page_size);

        $this->successReturn("获取成功",$return_data);
    }
}